//
//  MyBuyTogetherTableViewCell.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/9/11.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "MyBuyTogetherTableViewCell.h"

@implementation MyBuyTogetherTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setFrame:(CGRect)frame
{
	//frame.origin.x += 10;
	frame.origin.y += 10;
	
	//frame.size.width -= 20;
	frame.size.height -= 10;
	
	[super setFrame:frame];
}

- (void)setModel:(MyBuyTogetherModel *)model
{
	if (_model != model) {
		_model = model;
		OrderGoods *subModel = [[OrderGoods alloc] init];
		if (model.items.count) {
			[subModel setValuesForKeysWithDictionary:model.items];
			self.title.text = subModel.title;
			self.price.text = [NSString stringWithFormat:@"￥%.2lf", [model.total_price floatValue]/100];
			[self countTimeLeft];
			self.wantPeople.text = [NSString stringWithFormat:@"还差%@人拼单",model.surplus_people];
			[self.photo sd_setImageWithURL:[NSURL URLWithString:subModel.photo[0]]];
			if (subModel.selectedSpecs.count) {
				NSString *cateName = [subModel.selectedSpecs[0] valueForKey:@"name"];
				NSString *cateValue = [subModel.selectedSpecs[0] valueForKey:@"val"];
				self.specCate.text = [NSString stringWithFormat:@"%@: %@", cateName, cateValue];
				
				if (subModel.selectedSpecs.count >= 2) {
					NSString *specName = [subModel.selectedSpecs[1] valueForKey:@"name"];
					NSString *specValue = [subModel.selectedSpecs[1] valueForKey:@"val"];
					self.spec.text = [NSString stringWithFormat:@"%@: %@", specName, specValue];
				}
			}
		}
	}
}

- (void)countTimeLeft
{
	//时间戳转时间
	NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
	[formatter setDateFormat:@"HH:mm:ss"];
	
	NSTimeInterval endTime = [self.model.end_time doubleValue];
	NSTimeInterval nowTime = [[NSDate date] timeIntervalSince1970];
	NSTimeInterval difference = endTime - nowTime;
	int second = (int)difference %60;//秒
	int minute = (int)difference /60%60;
	int hour = (int)difference / 3600;
	//int day = (int)difference / (24 *3600);
	NSString *leftStr = [NSString stringWithFormat:@"剩余:%d:%d:%d", hour, minute, second];
	self.timeLeft.text = leftStr;
}
- (IBAction)shareToFriend:(id)sender {
	OrderGoods *subModel = [[OrderGoods alloc] init];
	if (self.model.items.count) {
		[subModel setValuesForKeysWithDictionary:self.model.items];
	}
	[self.delegate shareToFriend:subModel.goods_id :subModel.photo[0] :subModel.title];
}

@end
